﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

namespace DynamicGeometry
{
    public class StylePickerEditorFactory : BaseValueEditorFactory<StylePickerEditor>
    {
        public override bool SupportsValue(IValueProvider value)
        {
            return value.Type == typeof(IFigureStyle) && base.SupportsValue(value);
        }
    }

    public class StylePickerEditor : SelectorValueEditor, IValueEditor
    {
        /// <summary>
        /// 1. Get the type of the style being displayed
        /// 2. Get the list of all other styles like this one
        /// 3. Fill all the styles in the list
        /// </summary>
        public override void FillList()
        {
            IFigureStyle style = Value.GetValue<IFigureStyle>();
            IEnumerable<IFigureStyle> allStyles;
            FigureBase figure = Value.Parent as FigureBase;
            if (figure == null && style == null)
            {
                return;
            }
            if (figure != null)
            {
                allStyles = style.Drawing.StyleManager.GetSupportedStyles(figure);
            }
            else
            {
                allStyles = new[] { style };
            }
            var glyphs = allStyles.Select(s => GetGlyph(s));
            Items = glyphs.ToList();

            base.FillList();
        }

        protected override System.Windows.Controls.Primitives.Selector CreateSelector()
        {
            return new ListBox()
            {
                MaxHeight = 300
            };
        }

        FrameworkElement GetGlyph(IFigureStyle s)
        {
            var content = s.GetSampleGlyph();
            content.Margin = new Thickness(10);
            var result = new Grid();
            result.Children.Add(content);
            result.Tag = s;
            return result;
        }

        void StylePickerEditor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            UpdateSelectedItemGlyph();
        }

        protected override ValidationResult Validate(object value)
        {
            IFigureStyle style = (value as FrameworkElement).Tag as IFigureStyle;
            var result = new ValidationResult();
            if (style != null)
            {
                result.IsValid = true;
                result.Value = style;
            }
            return result;
        }

        public override void UpdateEditor()
        {
            //FillList();
            var value = GetValue();
            //UpdateSelectedItemGlyph();
            foreach (var item in Items)
            {
                if ((item as FrameworkElement).Tag == value)
                {
                    guard = true;
                    Selector.SelectedItem = item;
                    guard = false;
                }
            }
        }

        void UpdateSelectedItemGlyph()
        {
            Grid selectedItem = Selector.SelectedItem as Grid;
            if (selectedItem != null)
            {
                var style = selectedItem.Tag as IFigureStyle;
                if (style != null)
                {
                    var newGlyph = style.GetSampleGlyph();
                    newGlyph.Margin = new Thickness(10);
                    selectedItem.Children.Clear();
                    selectedItem.Children.Add(newGlyph);
                }
            }
        }
    }
}
